/* @formatter:off
 * Copyright (c) 2011 Lucas <lucas03093@gmail.com>
 * 
 * 
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
/* @formatter:on */

package nu.korkad.daytracker.activity;

import nu.korkad.daytracker.R;
import nu.korkad.daytracker.info.Dates;
import nu.korkad.daytracker.settings.Config;
import android.app.Activity;
import android.graphics.Color;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class ListView_Archive extends ArrayAdapter<String>
{
    private final Activity context;
    private final String[] company;
    private final String[] checkin;
    private final String[] checkout;
    private final String[] desc;
    private final String[] unread;
    private int            defaultTextColor = 0;
    
    public ListView_Archive(Activity context, String[] company, String[] checkin, String[] checkout, String[] desc, String[] unread)
    {
        super(context, R.layout.activity_archive_row_layout, company);
        
        this.context = context;
        this.company = company;
        this.checkin = checkin;
        this.checkout = checkout;
        this.desc = desc;
        this.unread = unread;
    }
    
    // Static
    static class ViewHolder
    {
        public TextView company;
        public TextView times;
        public TextView desc;
        public TextView id;
        public TextView timesum;
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        ViewHolder holder;
        
        // Recycle existing view if it passed the display
        View rowView = convertView;
        if (rowView == null)
        {
            LayoutInflater inflater = context.getLayoutInflater();
            rowView = inflater.inflate(R.layout.activity_archive_row_layout, null, true);
            holder = new ViewHolder();
            holder.company = (TextView) rowView.findViewById(R.id.layout_archive_row_company);
            holder.desc = (TextView) rowView.findViewById(R.id.layout_archive_row_desc);
            holder.times = (TextView) rowView.findViewById(R.id.layout_archive_row_times);
            holder.timesum = (TextView) rowView.findViewById(R.id.layout_archive_row_timesum);
            rowView.setTag(holder);
        }
        else
            holder = (ViewHolder) rowView.getTag();
        
        // Temp strings
        String company = this.company[position];
        String desc = this.desc[position]; // Desc
        String checkin = this.checkin[position];
        String checkintime = "";
        String checkindate = "";
        String checkout = this.checkout[position];
        String checkouttime = "";
        String checkoutdate = "";
        String date = "";
        long checkin_time = 0;
        long checkout_time = 0;
        
        // Convert dates
        if (!checkin.equals("0"))
        {
            checkin_time = Long.parseLong(checkin.trim()) * 1000;
            checkintime = DateFormat.getTimeFormat(context).format(checkin_time);
            checkindate = DateFormat.getDateFormat(context).format(checkin_time);
        }
        else
            checkin = "?";
        if (!checkout.equals("0"))
        {
            checkout_time = Long.parseLong(checkout.trim()) * 1000;
            checkouttime = DateFormat.getTimeFormat(context).format(checkout_time);
            checkoutdate = DateFormat.getDateFormat(context).format(checkout_time);
        }
        else
            checkout = "?";
        
        // Get correct date format
        // date time - time
        if (checkindate.equals(checkoutdate) && (checkin_time != 0 && checkout_time != 0))
            date = checkindate + " " + checkintime + " - " + checkouttime;
        // ? - date time
        else if (checkin_time == 0 && checkout_time != 0)
            date = "? - " + checkoutdate + " " + checkouttime;
        // date time - ?
        else if (checkout_time == 0 && checkin_time != 0)
            date = checkindate + " " + checkintime + " - ?";
        // date time - date time
        else if (checkout_time != 0 && checkin_time != 0)
            date = checkindate + " " + checkintime + " - " + checkoutdate + " " + checkouttime;
        // ? - ?
        else
            date = "? - ?";
        
        // Get short desc
        if (desc.length() > Config.ARCHIVE_REPORT_LIMIT)
            desc = desc.substring(0, desc.length() / 2) + "...";
        
        // Check company name
        if (company.equals(""))
            company = context.getString(R.string.empty);
        // Limit company name
        else if (company.length() > Config.ARCHIVE_COMPANY_NAME_LIMIT)
            company = company.substring(0, company.length() / 2) + "...";
        
        // Check report text
        if (desc.equals(""))
            desc = context.getString(R.string.empty);
        
        // Set stuff
        holder.company.setText(company);
        holder.desc.setText(desc);
        holder.times.setText(date);
        holder.timesum.setText(Dates.getDuration(checkin_time, checkout_time, context));
        
        // Set text colors if unread
        if (unread[position].equals("1"))
        {
            // Save default text color
            defaultTextColor = holder.company.getTextColors().getDefaultColor();
            
            // Change color of text
            holder.company.setTextColor(Color.rgb(218, 165, 32));
            holder.desc.setTextColor(Color.rgb(218, 165, 32));
            holder.times.setTextColor(Color.rgb(218, 165, 32));
            holder.timesum.setTextColor(Color.rgb(218, 165, 32));
        }
        else if (defaultTextColor != 0 && defaultTextColor != -2448096)
        {
            // Change color of text
            holder.company.setTextColor(defaultTextColor);
            holder.desc.setTextColor(defaultTextColor);
            holder.times.setTextColor(defaultTextColor);
            holder.timesum.setTextColor(defaultTextColor);
        }
        
        // Null them
        company = null;
        desc = null;
        checkin = null;
        checkintime = null;
        checkindate = null;
        checkout = null;
        checkouttime = null;
        checkoutdate = null;
        date = null;
        
        return rowView;
    }
}